package com.shop2cn.iapi.couponmanage.testcase.old;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.BagInfo;
import com.shop2cn.iapi.couponmanage.parameter.CouponBag;
import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateBean;
import com.shop2cn.iapi.couponmanage.parameter.GetBag;
import com.shop2cn.iapi.couponmanage.parameter.GetBagsByPaging;
import com.shop2cn.iapi.couponmanage.parameter.executeTaskBean;
import com.shop2cn.iapi.couponmanage.parameter.getBagBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCall;
import com.shop2cn.iapi.couponmanage.service.executeTaskCall;
import com.shop2cn.iapi.couponmanage.service.getBagCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponbag;
import com.ymttest.database.model.couponconversion;
import com.ymttest.database.model.couponconversioncode;
import com.ymttest.database.sqlwapper.couponbagWapper;
import com.ymttest.database.sqlwapper.couponconversionWapper;
import com.ymttest.database.sqlwapper.couponconversioncodeWapper;
import com.ymttest.database.sqlwapper.couponsendsubtaskWapper;
/**
*根据编号获取套券信息(支持批量)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getBag{
	private static getBagBean getbagBean;
	private static getBagCall getbagCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据编号获取套券信息(支持批量)");
	}

	@Before
	public void caseUp() {
		getbagBean=new getBagBean();
		getbagCall=new getBagCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getbagCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getBag_001() {
		Logger.start(false,"根据编号获取套券信息-套券没有子批次");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponShop2cnMCallService.createPlatformCouponBag(createBagBean);
			List<String> list=new ArrayList();
			list.add(createBagCall.getbagCode());
			getbagBean.setBagCodes(list);
			getbagCall.setData(getbagBean);
			getbagCall.callService();	
			verifyReturn();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getBag_002() {
		Logger.start(true,"根据编号获取套券信息-套券中的子批次是随机兑换批次");
		try {
			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = mCouponserviceCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			createBagBean.setAcquireMethod(2);//2：领取方式是套券
			CreateBagCall createBagCall = mCouponserviceCallService.createPlatformCouponBag(createBagBean);
			
			//生成批次及兑换码
			List<String> bagCodes = new ArrayList<>();
			bagCodes.add(createBagCall.getbagCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultRandomCreateBean(bagCodes);
			createBean.setBatchType(2);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 这里直接调用job接口生成兑换码
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			couponconversion bill = new couponconversionWapper()
					.selectByconversionName(createBean.getBillName()).get(0);
			List<couponconversioncode> CouponConversionCodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
			List<String> list=new ArrayList();
			list.add(createBagCall.getbagCode());
			getbagBean.setBagCodes(list);
			getbagCall.setData(getbagBean);
			getbagCall.callService();	
			verifyReturn();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getBag_003() {
		Logger.start(true,"根据编号获取套券信息-套券中的子批次是自定义兑换批次");
		try {
			CouponShop2cnMCallService mCouponserviceCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = mCouponserviceCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			createBagBean.setAcquireMethod(2);//领取方式是套券
			CreateBagCall createBagCall = mCouponserviceCallService.createPlatformCouponBag(createBagBean);
			
			//生成批次及兑换码
			List<String> bagCodes = new ArrayList<>();
			bagCodes.add(createBagCall.getbagCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(bagCodes);
			createBean.setBatchType(2);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 这里直接调用job接口生成兑换码
			List<Map>  tasks=new couponsendsubtaskWapper().selectTaskByTaskTypelimit(3,1);
			executeTaskCall call=new executeTaskCall();
			executeTaskBean bean=new executeTaskBean();
			
			for(int i=0;i<tasks.size();i++){
				bean.setSubTaskId(Integer.parseInt(tasks.get(i).get("SubTaskId").toString()));
				call.setData(bean);
				call.callService();
			}
			couponconversion bill = new couponconversionWapper()
					.selectByconversionName(createBean.getBillName()).get(0);
			List<couponconversioncode> CouponConversionCodes = new couponconversioncodeWapper().selectByConversionId(bill.getConversionId());
			
			List<String> list=new ArrayList();
			list.add(createBagCall.getbagCode());
			getbagBean.setBagCodes(list);
			getbagCall.setData(getbagBean);
			getbagCall.callService();	
			verifyReturn();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getBag_005() {
		Logger.start(true,"根据编号获取套券信息-bagCodes数量>1");
		try {
			CouponShop2cnMCallService CouponShop2cnMCallService = new CouponShop2cnMCallService();
			CreateBagBean createBagBean = CouponShop2cnMCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
			CreateBagCall createBagCall = CouponShop2cnMCallService.createPlatformCouponBag(createBagBean);
			CreateBagCall createBagCall2 = CouponShop2cnMCallService.createPlatformCouponBag(createBagBean);
			List<String> list=new ArrayList();
			list.add(createBagCall.getbagCode());
			list.add(createBagCall2.getbagCode());
			getbagBean.setBagCodes(list);
			getbagCall.setData(getbagBean);
			getbagCall.callService();	
			verifyReturn();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Ts_getBag_006() {
		Logger.start(true,"根据编号获取套券信息-bagCodes为空集合");
		try {
			List<String> list=new ArrayList();
			getbagBean.setBagCodes(list);
			
			getbagCall.setData(getbagBean);
			getbagCall.callService();	
			//verifyReturn();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	private void verifyReturn()
	{
		Logger.verifyEquals("0", getbagCall.getStatusCode1(), "验证返回code");
        Logger.verifyEquals("操作成功！", getbagCall.getMessage(), "验证返回Message");
        
        List<couponbag> couponbags=new couponbagWapper().selectByBagCodes(getbagBean.getBagCodes());
        GetBag getbag=getbagCall.getBagReturnData();
        List<CouponBag> bagInfos=getbag.getBags();

        Logger.verifyEquals(couponbags.size(), bagInfos.size(), "验证返回bags的条数与数据库couponbag表中的套券总条数一致");

        for(int i=0;i<bagInfos.size();i++)
        {
		 int count=+1;
		 couponbag bag=couponbags.get(i);
		 CouponBag info=bagInfos.get(i);
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 
		 Logger.verifyEquals(bag.getBagCode(), info.getBagCode(), "验证返回第"+count+"条bagcode与数据库couponbag表中的bagcode字段一致");
		 Logger.verifyEquals(bag.getName(), info.getName(), "验证返回第"+count+"条name与数据库couponbag表中的name字段一致");
		 Logger.verifyEquals(bag.getDescription(), info.getDescription(), "验证返回第"+count+"条description与数据库couponbag表中的description字段一致");
		 Logger.verifyEquals(bag.getOperatorId(), info.getOperatorId(), "验证返回第"+count+"条operatorId与数据库couponbag表中的operatorId字段一致");
		 Logger.verifyEquals(bag.getApplyUser(), info.getApplyUser(), "验证返回第"+count+"条ApplyUser与数据库couponbag表中的ApplyUser字段一致");
		 Logger.verifyEquals(bag.getApplyMemo(), info.getApplyMemo(), "验证返回第"+count+"条ApplyMemo与数据库couponbag表中的ApplyMemo字段一致");
		 Logger.verifyEquals(bag.getEffectiveType(), info.getEffectiveType(), "验证返回第"+count+"条effectiveType与数据库couponbag表中的effectiveType字段一致");
		 Logger.verifyEquals(bag.getEffectiveDays(), info.getEffectiveDays(), "验证返回第"+count+"条effectiveDays与数据库couponbag表中的effectiveDays字段一致");
		 Logger.verifyEquals(bag.getEffectiveValidType(), info.getEffectiveValidType(), "验证返回第"+count+"条effectiveValidType与数据库couponbag表中的effectiveValidType字段一致");
		 Logger.verifyEquals(sdf.format(bag.getValidStart()), sdf.format(info.getValidStart()), "验证返回第"+count+"条effectiveStartTime与数据库couponbag表中的ValidStart字段一致");
		 Logger.verifyEquals(sdf.format(bag.getValidEnd()), sdf.format(info.getValidEnd()), "验证返回第"+count+"条ValidEnd与数据库couponbag表中的EffectiveEndTime字段一致");
		 Logger.verifyEquals(sdf.format(bag.getAcquireStartTime()), sdf.format(info.getAcquireStartTime()), "验证返回第"+count+"条AcquireStartTime与数据库couponbag表中的AcquireStartTime字段一致");
		 Logger.verifyEquals(sdf.format(bag.getAcquireEndTime()), sdf.format(info.getAcquireEndTime()), "验证返回第"+count+"条acquireEndTime与数据库couponbag表中的acquireEndTime字段一致");
		 Logger.verifyEquals(bag.getAcquireMethod(), info.getAcquireMethod(), "验证返回第"+count+"条acquireMethod与数据库couponbag表中的acquireMethod字段一致");
		 Logger.verifyEquals(bag.getTotalNum(), info.getTotalNum(), "验证返回第"+count+"条TotalNum与数据库couponbag表中的TotalNum字段一致");
		 Logger.verifyEquals(bag.getReceivedCount(), info.getReceivedQuantity(), "验证返回第"+count+"条ReceivedCount与数据库couponbag表中的ReceivedQuantity字段一致");
		 Logger.verifyEquals(bag.getReceiveTimesPerUser(), info.getReceiveTimesPerUser(), "验证返回第"+count+"条ReceiveTimesPerUser与数据库couponbag表中的ReceiveTimesPerUser字段一致");
		 Logger.verifyEquals(bag.getReceiveUserType(), info.getReceiveUserType(), "验证返回第"+count+"条ReceiveUserType与数据库couponbag表中的ReceiveUserType字段一致");
		 Logger.verifyEquals(bag.getUsePlatformIds(), info.getUsePlatformIds(), "验证返回第"+count+"条UsePlatformIds与数据库couponbag表中的UsePlatformIds字段一致");
		 Logger.verifyEquals(bag.getTotalAmount().stripTrailingZeros().toPlainString(), info.getTotalAmount().toString(), "验证返回第"+count+"条totalAmount与数据库couponbag表中的totalAmount字段一致");
		 Logger.verifyEquals(bag.getImpresetInvoiceId(), info.getImpresetInvoiceId(), "验证返回第"+count+"条ImpresetInvoiceId与数据库couponbag表中的ImpresetInvoiceId字段一致");
		 Logger.verifyEquals(bag.getApplyDepartment(), info.getApplyDepartment(), "验证返回第"+count+"条ApplyDepartment与数据库couponbag表中的ApplyDepartment字段一致");
		 Logger.verifyEquals(sdf.format(bag.getAddTime()), sdf.format(info.getCreateTime()), "验证返回第"+count+"条createTime与数据库couponbag表中的AddTime字段一致");
		 
        }
	}
}